package top.bugger168.cache_task_email_msg_admin.util;

import lombok.extern.slf4j.Slf4j;
import org.bson.types.Code;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CodeUtils {
    private final String[] patch = {"000000","00000","0000","000","00","0",""};

    public String generator(String tele){
        // 输入手机号,获取到验证码
        int hash = tele.hashCode();
        int encryption = 20206666;
        long result = hash ^ encryption;
        long nowTime = System.currentTimeMillis();
        result = result ^ nowTime;
        long code = result % 1000000;
        code = code < 0 ? -code : code;
        String codeStr = code + "";
        int len = codeStr.length();
        return patch[len] + codeStr;
    }

    // 取用缓存数据要靠spring来代理,取用其他方法存入缓存的数据需要将方法放入spring管理的bean中(可以给类加component注解)
    // 这个方法我返回null,如果缓存里有就直接返回缓存的验证码
    @Cacheable(value = "smsCode", key = "#tele")
    public String get(String tele){
        return null;
    }

}
